
package com.dustedpixels.jasmin.unit.compiler;

import java.util.regex.Matcher;

public class UpdateEndPointsLine {
  public enum Type{UPDATE_END_POINTS_CALL, OTHER};
  
  public Type type;
  public String updateEndPointsTarget;
  public String code;
  
  
  public UpdateEndPointsLine replaceLocalFieldAccess(Unit unit, String targetName) {
    String compiledCode = code;
    for (FieldLine fieldLine : unit.fieldLines) {
      compiledCode = Regex.getFieldAccessPattern(fieldLine.name).matcher(compiledCode).replaceAll(targetName + "_$1");
    }
    
    UpdateEndPointsLine compiledLine = new UpdateEndPointsLine();
    compiledLine.type = type;
    compiledLine.updateEndPointsTarget = updateEndPointsTarget;
    compiledLine.code = compiledCode;
    return compiledLine;
  }
  
  public UpdateEndPointsLine replacedUnitFieldAccess(Unit unit) {
    String compiledCode = code;
    for (String unitFieldName : unit.unitFieldNames) {
      compiledCode = Regex.getUnitFieldAccessPattern(unitFieldName).matcher(compiledCode).replaceAll("$1_$3");
    }

    UpdateEndPointsLine compiledLine = new UpdateEndPointsLine();
    compiledLine.type = type;
    compiledLine.updateEndPointsTarget = updateEndPointsTarget;
    compiledLine.code = compiledCode;
    return compiledLine;
  }
}
